<?php

/**
 * 行为分析-埋点-分支协议
 *
 * @author meng.deng
 */

require_once 'CommonController.php';

class Admin_ActMdChildController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_mainPactModel = new Application_Model_ActMdMainPact();
        $this->_mainPactEventModel = new Application_Model_ActMdMainPactEvent();
        $this->_mainPactEventFieldModel = new Application_Model_ActMdMainPactEventField();
        $this->_mainPactIdDefineModel = new Application_Model_ActMdMainPactIdDefine();
        $this->_mainPactEventFieldEnumModel = new Application_Model_ActMdMainPactEventFieldEnum();
        $this->_mainPactEventFieldLoopModel = new Application_Model_ActMdMainPactEventFieldLoop();
        $this->_childPactModel = new Application_Model_ActMdChildPact();
        $this->_childPactEventModel = new Application_Model_ActMdChildPactEvent();
        $this->_childPactEventFieldModel = new Application_Model_ActMdChildPactEventField();
        $this->_childPactIdDefineModel = new Application_Model_ActMdChildPactIdDefine();
        $this->_childPactEventFieldEnumModel = new Application_Model_ActMdChildPactEventFieldEnum();
        $this->_childPactEventFieldLoopModel = new Application_Model_ActMdChildPactEventFieldLoop();
        $this->_fieldTypeModel = new Application_Model_ActMdFieldType();
        
        $this->_childPactPageIdModel = new Application_Model_ActMdChildPactPageId();
        
        $this->_productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {
        //获取当前页码
        $pageSize = 30;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();
        $wheres[] = "`productCode` = '{$this->_productCode}'";
        
        $urls = $vars;

        $orderBy = '`addTime` DESC';

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_childPactModel->getTableName()}`{$where} ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        
        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
        
    }
    
    /**
     * 添加动作
     * 
     * @return void
     */
    public function childInsertAction()
    {
        $this->view->productCode= $this->_productCode;
        $curUser = Application_Model_UserLogin::getInstance()->getLoginedUserName();
        $this->view->pactAuthor= $curUser; 
        //主协议版本
        $mainRows = $this->_mainPactModel->fetchAll('`pactStatus`=1');
        $this->view->mainRows = $mainRows;
        //操作系统
        $pagevisitModel = new Application_Model_ActDayDataPagevisit();
        $osNameRows = $pagevisitModel ->getOsNameArray();
        $this->view->osNameRows = $osNameRows;
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            
            $field['productCode'] = $filter->filter(trim($this->_request->getPost('productCode')));
            $field['osName'] = $filter->filter(trim($this->_request->getPost('osName')));
            $field['childVersion'] = trim($this->_request->getPost('childVersion'));
            $field['childVersion'] = $field['productCode'].'-'.$field['osName'].'-'.$field['childVersion'];
            $field['histroyVersion'] = '1';//$filter->filter(trim($this->_request->getPost('histroyVersion')));
            $field['childStatus'] = $filter->filter(trim($this->_request->getPost('childStatus')));
            $field['addTime']= time();
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));
            $field['pactAuthor'] = $filter->filter(trim($this->_request->getPost('pactAuthor')));

            if (strlen($field['mainVersion']) ==0) {
                echo $this->view->message('请选择主协议版本。') ;
                exit;
            }
            if (strlen($field['childVersion']) ==0) {
                echo $this->view->message('请输入分支协议版本。') ;
                exit;
            }
            if ($this->_childPactModel->isExists($field['productCode'],$field['osName'],$field['childVersion'])) {
                echo $this->view->message('分支协议版本已经存在，请重新填写。') ;
                exit; 
            }
            //添加
            $id = $this->_childPactModel->insert($field);
            if($id>0){
                //添加事件
                $mainEventRows = $this->_mainPactEventModel->fetchAll("`mainVersion`='{$field['mainVersion']}'");
                foreach ($mainEventRows as $row){
                    $doArray = array();
                    $doArray['childVersion'] = $field['childVersion'];
                    $doArray['eventId'] = $row['eventId'];
                    $doArray['eventName'] = $row['eventName'];
                    $doArray['eventBagName'] = $row['eventBagName'];
                    $doArray['eventComplyMode'] = $row['eventComplyMode'];
                    $doArray['eventUploadMode'] = $row['eventUploadMode'];
                    $doArray['eventPoint'] = $row['eventPoint'];
                    $doArray['addTime']= time();
                    $doArray['note'] = $row['note'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_childPactEventModel->getTableName()}` WHERE `childVersion`='{$doArray['childVersion']}' AND `eventId`={$doArray['eventId']}";
                    $row = $this->_childPactEventModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_childPactEventModel->insert($doArray);
                    }
                }
                //添加字段
                $mainEventFieldRows = $this->_mainPactEventFieldModel->fetchAll("`mainVersion`='{$field['mainVersion']}'");
                foreach ($mainEventFieldRows as $row){
                    $doArray = array();
                    $doArray['childVersion'] = $field['childVersion'];
                    $doArray['eventId'] = $row['eventId'];
                    $doArray['fieldName'] = $row['fieldName'];
                    $doArray['fieldSort'] = $row['fieldSort'];
                    $doArray['complyMode'] = $row['complyMode'];
                    $doArray['fieldType'] = $row['fieldType'];
                    $doArray['addTime']= time();
                    $doArray['note'] = $row['note'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_childPactEventFieldModel->getTableName()}` WHERE `childVersion`='{$doArray['childVersion']}' AND `eventId`={$doArray['eventId']} AND `fieldSort` ={$doArray['fieldSort']}";
                    $row = $this->_childPactEventFieldModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_childPactEventFieldModel->insert($doArray);
                    }
                }
                //添加Id定义
                $mainIdDefineRows = $this->_mainPactIdDefineModel->fetchAll("`mainVersion`='{$field['mainVersion']}'");
                foreach ($mainIdDefineRows as $row){
                    $doArray = array();
                    $doArray['childVersion'] = $field['childVersion'];
                    $doArray['fieldType'] = $row['fieldType'];
                    $doArray['pageId'] = $row['pageId'];
                    $doArray['pageName'] = $row['pageName'];
                    $doArray['childPageName'] = $row['pageName'];
                    $doArray['pageType'] = 0;
                    $doArray['relationId'] = $row['relationId'];
                    $doArray['aidNote'] = $row['aidNote'];
                    $doArray['isUse'] = $row['isUse'];
                    $doArray['isUseBi'] = $row['isUseBi'];
                    $doArray['isUseManager'] = 0;
                    $doArray['isUserNow'] = 0;
                    $doArray['isFlag'] = $row['isFlag'];
                    $doArray['addTime']= time();
                    $doArray['note'] = $row['note'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_childPactIdDefineModel->getTableName()}` WHERE `childVersion`='{$doArray['childVersion']}' AND `fieldType`='{$doArray['fieldType']}' AND `pageId`={$doArray['pageId']}";
                    $row = $this->_childPactIdDefineModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_childPactIdDefineModel->insert($doArray);
                    }
                }
                //添加枚举
                $mainFieldEnumRows = $this->_mainPactEventFieldEnumModel->fetchAll("`mainVersion`='{$field['mainVersion']}'");
                foreach ($mainFieldEnumRows as $row){
                    $doArray = array();
                    $doArray['childVersion'] = $field['childVersion'];
                    $doArray['eventId'] = $row['eventId'];
                    $doArray['fieldSort'] = $row['fieldSort'];
                    $doArray['fieldType'] = $row['fieldType'];
                    $doArray['enum'] = $row['enum'];
                    $doArray['enumValue'] = $row['enumValue'];
                    $doArray['enumAuthor'] = $curUser;
                    $doArray['addTime']= time();
                    $doArray['remark'] = $row['remark'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_childPactEventFieldEnumModel->getTableName()}` WHERE `childVersion`='{$doArray['childVersion']}' AND `eventId`={$doArray['eventId']} AND `fieldSort`={$doArray['fieldSort']} AND `enum`={$doArray['enum']}";
                    $row = $this->_childPactEventFieldEnumModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_childPactEventFieldEnumModel->insert($doArray);
                    }
                }
                //添加循环类型
                $mainFieldLoopRows = $this->_mainPactEventFieldLoopModel->fetchAll("`mainVersion`='{$field['mainVersion']}'");
                foreach ($mainFieldLoopRows as $row){
                    $doArray = array();
                    $doArray['childVersion'] = $field['childVersion'];
                    $doArray['eventId'] = $row['eventId'];
                    $doArray['fieldSort'] = $row['fieldSort'];
                    $doArray['loopFieldSort'] = $row['loopFieldSort'];
                    $doArray['fieldName'] = $row['fieldName'];
                    $doArray['fieldType'] = $row['fieldType'];
                    $doArray['complyMode'] = $row['complyMode'];
                    $doArray['addTime']= time();
                    $doArray['note'] = $row['note'];
                    $doArray['remark'] = $row['remark'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_childPactEventFieldLoopModel->getTableName()}` WHERE `childVersion`='{$doArray['childVersion']}' AND `eventId`={$doArray['eventId']} AND `fieldSort`={$doArray['fieldSort']} AND `loopFieldSort`={$doArray['loopFieldSort']}";
                    $row = $this->_childPactEventFieldLoopModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_childPactEventFieldLoopModel->insert($doArray);
                    }
                }
            
            }
            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'index'))) ;
            exit;
        }
    }
    
   /**
     * 复制动作
     * 
     * @return void
     */
    public function childCopyAction()
    {
        $this->view->productCode= $this->_productCode;
        $curUser = Application_Model_UserLogin::getInstance()->getLoginedUserName();
        $this->view->pactAuthor= $curUser;
        //分支协议版本
        $childRows = $this->_childPactModel->fetchAll(" `productCode`='{$this->_productCode}'  AND `childStatus`=1");
        $this->view->childRows = $childRows;
        //操作系统
        $pagevisitModel = new Application_Model_ActDayDataPagevisit();
        $osNameRows = $pagevisitModel ->getOsNameArray();
        $this->view->osNameRows = $osNameRows;
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $copyChildVersion = trim($this->_request->getPost('copyChildVersion'));
            $childRow = $this->_childPactModel->fetchRow(" `childVersion`='{$copyChildVersion}'");
            $field['mainVersion'] = $childRow['mainVersion'];
            $field['productCode'] = $filter->filter(trim($this->_request->getPost('productCode')));
            $field['osName'] = $filter->filter(trim($this->_request->getPost('osName')));
            $field['childVersion'] = trim($this->_request->getPost('childVersion'));
            $field['childVersion'] = $field['productCode'].'-'.$field['osName'].'-'.$field['childVersion'];
            $field['histroyVersion'] = '1';//$filter->filter(trim($this->_request->getPost('histroyVersion')));
            $field['childStatus'] = $filter->filter(trim($this->_request->getPost('childStatus')));
            $field['addTime']= time();
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));
            $field['pactAuthor'] = $filter->filter(trim($this->_request->getPost('pactAuthor')));

            if (strlen($field['mainVersion']) ==0) {
                echo $this->view->message('复制协议版本有误，无法进行本次操作。') ;
                exit;
            }
            if (strlen($field['childVersion']) ==0) {
                echo $this->view->message('请输入分支协议版本。') ;
                exit;
            }
            if ($this->_childPactModel->isExists($field['productCode'],$field['osName'],$field['childVersion'])) {
                echo $this->view->message('分支协议版本已经存在，请重新填写。') ;
                exit; 
            }
            //添加
            $id = $this->_childPactModel->insert($field);
            if($id>0){
                //添加事件
                $childEventRows = $this->_childPactEventModel->fetchAll("`childVersion`='{$copyChildVersion}'");
                foreach ($childEventRows as $row){
                    $doArray = array();
                    $doArray['childVersion'] = $field['childVersion'];
                    $doArray['eventId'] = $row['eventId'];
                    $doArray['eventName'] = $row['eventName'];
                    $doArray['eventBagName'] = $row['eventBagName'];
                    $doArray['eventComplyMode'] = $row['eventComplyMode'];
                    $doArray['eventUploadMode'] = $row['eventUploadMode'];
                    $doArray['eventPoint'] = $row['eventPoint'];
                    $doArray['addTime']= time();
                    $doArray['note'] = $row['note'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_childPactEventModel->getTableName()}` WHERE `childVersion`='{$doArray['childVersion']}' AND `eventId`={$doArray['eventId']}";
                    $row = $this->_childPactEventModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_childPactEventModel->insert($doArray);
                    }
                }
                //添加字段
                $childEventFieldRows = $this->_childPactEventFieldModel->fetchAll("`childVersion`='{$copyChildVersion}'");
                foreach ($childEventFieldRows as $row){
                    $doArray = array();
                    $doArray['childVersion'] = $field['childVersion'];
                    $doArray['eventId'] = $row['eventId'];
                    $doArray['fieldName'] = $row['fieldName'];
                    $doArray['fieldSort'] = $row['fieldSort'];
                    $doArray['complyMode'] = $row['complyMode'];
                    $doArray['fieldType'] = $row['fieldType'];
                    $doArray['addTime']= time();
                    $doArray['note'] = $row['note'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_childPactEventFieldModel->getTableName()}` WHERE `childVersion`='{$doArray['childVersion']}' AND `eventId`={$doArray['eventId']} AND `fieldSort` ={$doArray['fieldSort']}";
                    $row = $this->_childPactEventFieldModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_childPactEventFieldModel->insert($doArray);
                    }
                }
                //添加Id定义
                $childIdDefineRows = $this->_childPactIdDefineModel->fetchAll("`childVersion`='{$copyChildVersion}'");
                foreach ($childIdDefineRows as $row){
                    $doArray = array();
                    $doArray['childVersion'] = $field['childVersion'];
                    $doArray['fieldType'] = $row['fieldType'];
                    $doArray['pageId'] = $row['pageId'];
                    $doArray['pageName'] = $row['pageName'];
                    $doArray['childPageName'] = $row['childPageName'];
                    $doArray['pageType'] = $row['pageType'];
                    $doArray['relationId'] = $row['relationId'];
                    $doArray['aidNote'] = $row['aidNote'];
                    $doArray['isUse'] = $row['isUse'];
                    $doArray['isUseBi'] = $row['isUseBi'];
                    $doArray['isUseManager'] = $row['isUseManager'];
                    $doArray['isUserNow'] = $row['isUserNow'];
                    $doArray['isFlag'] = $row['isFlag'];
                    $doArray['addTime']= time();
                    $doArray['note'] = $row['note'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_childPactIdDefineModel->getTableName()}` WHERE `childVersion`='{$doArray['childVersion']}' AND `fieldType`='{$doArray['fieldType']}' AND `pageId`={$doArray['pageId']}";
                    $row = $this->_childPactIdDefineModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_childPactIdDefineModel->insert($doArray);
                    }
                }
                //添加枚举
                $childFieldEnumRows = $this->_childPactEventFieldEnumModel->fetchAll("`childVersion`='{$copyChildVersion}'");
                foreach ($childFieldEnumRows as $row){
                    $doArray = array();
                    $doArray['childVersion'] = $field['childVersion'];
                    $doArray['eventId'] = $row['eventId'];
                    $doArray['fieldSort'] = $row['fieldSort'];
                    $doArray['fieldType'] = $row['fieldType'];
                    $doArray['enum'] = $row['enum'];
                    $doArray['enumValue'] = $row['enumValue'];
                    $doArray['enumAuthor'] = $curUser;
                    $doArray['addTime']= time();
                    $doArray['remark'] = $row['remark'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_childPactEventFieldEnumModel->getTableName()}` WHERE `childVersion`='{$doArray['childVersion']}' AND `eventId`={$doArray['eventId']} AND `fieldSort`={$doArray['fieldSort']} AND `enum`={$doArray['enum']}";
                    $row = $this->_childPactEventFieldEnumModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_childPactEventFieldEnumModel->insert($doArray);
                    }
                }
                //添加循环类型
                $childFieldLoopRows = $this->_childPactEventFieldLoopModel->fetchAll("`childVersion`='{$copyChildVersion}'");
                foreach ($childFieldLoopRows as $row){
                    $doArray = array();
                    $doArray['childVersion'] = $field['childVersion'];
                    $doArray['eventId'] = $row['eventId'];
                    $doArray['fieldSort'] = $row['fieldSort'];
                    $doArray['loopFieldSort'] = $row['loopFieldSort'];
                    $doArray['fieldName'] = $row['fieldName'];
                    $doArray['fieldType'] = $row['fieldType'];
                    $doArray['complyMode'] = $row['complyMode'];
                    $doArray['addTime']= time();
                    $doArray['note'] = $row['note'];
                    $doArray['remark'] = $row['remark'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_childPactEventFieldLoopModel->getTableName()}` WHERE `childVersion`='{$doArray['childVersion']}' AND `eventId`={$doArray['eventId']} AND `fieldSort`={$doArray['fieldSort']} AND `loopFieldSort`={$doArray['loopFieldSort']}";
                    $row = $this->_childPactEventFieldLoopModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_childPactEventFieldLoopModel->insert($doArray);
                    }
                }
                //添加页面ID
                $childPageIdRows = $this->_childPactPageIdModel->fetchAll("`childVersion`='{$copyChildVersion}'");
                foreach ($childPageIdRows as $row){
                    $doArray = array();
                    $doArray['childVersion'] = $field['childVersion'];
                    $doArray['fieldType'] = $row['fieldType'];
                    $doArray['pageId'] = $row['pageId'];
                    $doArray['pageName'] = $row['pageName'];
                    $doArray['pageType'] = $row['pageType'];
                    $doArray['relationId'] = $row['relationId'];
                    $doArray['isMd'] = $row['isMd'];
                    $doArray['isUseBi'] = $row['isUseBi'];
                    $doArray['isUseManager'] = $row['isUseManager'];
                    $doArray['isUserNow'] = $row['isUserNow'];
                    $doArray['isFlag'] = $row['isFlag'];
                    $doArray['isLock'] = 0;
                    $doArray['mdVersion'] = $row['mdVersion'];
                    $doArray['addTime']= time();
                    $doArray['note'] = $row['note'];
                    $doArray['remark'] = $row['remark'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_childPactPageIdModel->getTableName()}` WHERE `childVersion`='{$doArray['childVersion']}' AND `fieldType`='{$doArray['fieldType']}' AND `pageId`={$doArray['pageId']}";
                    $row = $this->_childPactPageIdModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_childPactPageIdModel->insert($doArray);
                    }
                }
                
                
                
                
            
            }
            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'index'))) ;
            exit;
        }
    }
    
   /**
     * 更新动作
     * 
     * @return void
     */
    public function childUpdateAction()
    {
        //操作系统
        $pagevisitModel = new Application_Model_ActDayDataPagevisit();
        $osNameRows = $pagevisitModel ->getOsNameArray();
        $this->view->osNameRows = $osNameRows;
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_childPactModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;

        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = trim($this->_request->getPost('mainVersion'));
            $field['productCode'] = trim($this->_request->getPost('productCode'));
            $field['osName'] = $filter->filter(trim($this->_request->getPost('osName')));
            $field['childVersion'] = trim($this->_request->getPost('childVersion'));
            //$field['histroyVersion'] = $filter->filter(trim($this->_request->getPost('histroyVersion')));
            $field['childStatus'] = $filter->filter(trim($this->_request->getPost('childStatus')));
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));

            if (strlen($field['mainVersion']) ==0) {
                echo $this->view->message('请选择主协议版本。') ;
                exit;
            }
            if (strlen($field['childVersion']) ==0) {
                echo $this->view->message('请输入分支协议版本。') ;
                exit;
            }
            if ($this->_childPactModel->isExists($field['productCode'],$field['osName'],$field['childVersion'], $row['id'])) {
                echo $this->view->message("分支协议版本[{$field['childVersion']}]已经存在，请重新填写。") ;
                exit; 
            }
            //修改
            $num = $this->_childPactModel->update($field, "`id` = {$id}");
            if ($num>0){
            	$element = array();
                if ($field['childStatus']==1){
                    $element['isLock'] = 1;
                    $this->_childPactPageIdModel->update($element,"`childVersion` = '{$row['childVersion']}'");
                } else{
                    $element['isLock'] = 0;
                    $this->_childPactPageIdModel->update($element,"`childVersion` = '{$row['childVersion']}'");
                }
            }
            
            $passwordMsg = ",分支版本{$field['childVersion']}信息已更新";

            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功{$passwordMsg}。", $backUrl) ;
            exit;
        }
    }

    /**
     * 删除动作
     * 
     * @return void
     */
    public function childDeleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            //清空字段
            foreach ($ids as $id){
                $row = $this->_childPactModel->fetchRow("`id` = {$id}");
                if ($row){
                    $this->_childPactEventModel->delete(" `childVersion`='{$row['childVersion']}'");
                    $this->_childPactEventFieldModel->delete(" `childVersion`='{$row['childVersion']}'");
                    $this->_childPactIdDefineModel->delete(" `childVersion`='{$row['childVersion']}'");
                    $this->_childPactEventFieldEnumModel->delete(" `childVersion`='{$row['childVersion']}'");
                    $this->_childPactEventFieldLoopModel->delete(" `childVersion`='{$row['childVersion']}'");
                    $this->_childPactPageIdModel->delete(" `childVersion`='{$row['childVersion']}'");
                }
            }
            //删除事件
            if (is_array($ids)) {
                $this->_childPactModel->deleteByIds($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
    
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getChildPactVersionIsExistsByJsonAction()
    {
        $productCode = trim($this->_request->get('productCode'));
        $osName = trim($this->_request->get('osName'));
        $account = trim($this->_request->get('account'));
        $id= intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_childPactModel->isExists($productCode,$osName,$account, $id);
        }
        exit(Zend_Json::encode($isExists));
    }
    
  
    /**
     * 主协议事件列表
     * 
     * @return void
     */
    public function childEventAction()
    {
        //获取当前页码
        $pageSize = 2000;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));

        $urls = $vars;
        if ($vars['childVersion'] != '') {
            $wheres[] = "`childVersion`= '{$vars['childVersion']}'";
        }
        $orderBy = '`eventId` ASC';

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_childPactEventModel->getTableName()}`{$where} ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        
        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
    }
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getChildPactEventIdIsExistsByJsonAction()
    {
        $childVersion = trim($this->_request->get('childVersion'));
        $account = trim($this->_request->get('account'));
        $id = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_childPactEventModel->isExists($childVersion,$account, $id);
        }
        exit(Zend_Json::encode($isExists));
    }
    
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getChildPactEventBagNameIsExistsByJsonAction()
    {
        $childVersion = trim($this->_request->get('childVersion'));
        $account = trim($this->_request->get('account'));
        $id = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_childPactEventModel->isExistsByEventBagName($childVersion,$account, $id);
        }
        exit(Zend_Json::encode($isExists));
    }
    
    
   /**
     * 添加动作
     * 
     * @return void
     */
    public function childEventInsertAction()
    {
        $vars = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['childVersion'] = $filter->filter(trim($this->_request->getPost('childVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['eventName'] = $filter->filter(trim($this->_request->getPost('eventName')));
            $field['eventBagName'] = $filter->filter(trim($this->_request->getPost('eventBagName')));
            $field['eventComplyMode'] = (int)$filter->filter(trim($this->_request->getPost('eventComplyMode')));
            $field['eventUploadMode'] = (int)$filter->filter(trim($this->_request->getPost('eventUploadMode')));
            $field['eventPoint'] = $filter->filter(trim($this->_request->getPost('eventPoint')));
            $field['addTime']= time();
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));

            if (strlen($field['eventId']) ==0) {
                echo $this->view->message('请输入事件Id。') ;
                exit;
            }
            if ($this->_childPactEventModel->isExists($field['childVersion'],$field['eventId'])) {
                echo $this->view->message('此协议版本事件Id已经存在，请重新填写。') ;
                exit; 
            }
            if (strlen($field['eventBagName']) ==0) {
                echo $this->view->message('请输入事件别名。') ;
                exit;
            }
            if ($this->_childPactEventModel->isExistsByEventBagName($field['childVersion'],$field['eventBagName'])) {
                echo $this->view->message('此协议版本事件别名已经存在，请重新填写。') ;
                exit; 
            }
            $id = $this->_childPactEventModel->insert($field);

            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'child-event','childVersion' => $field['childVersion']))) ;
            exit;
        }
    }
    
   /**
     * 更新动作
     * 
     * @return void
     */
    public function childEventUpdateAction()
    {
        $vars = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_childPactEventModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;

        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['childVersion'] = $filter->filter(trim($this->_request->getPost('childVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['eventName'] = $filter->filter(trim($this->_request->getPost('eventName')));
            $field['eventBagName'] = $filter->filter(trim($this->_request->getPost('eventBagName')));
            $field['eventComplyMode'] = (int)$filter->filter(trim($this->_request->getPost('eventComplyMode')));
            $field['eventUploadMode'] = (int)$filter->filter(trim($this->_request->getPost('eventUploadMode')));
            $field['eventPoint'] = $filter->filter(trim($this->_request->getPost('eventPoint')));
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));

            if (strlen($field['eventId']) ==0) {
                echo $this->view->message('请输入事件Id。') ;
                exit;
            }
            if ($this->_childPactEventModel->isExists($field['childVersion'],$field['eventId'], $row['id'])) {
                echo $this->view->message("协议版本[{$field['mainVersion']}]事件[{$field['eventId']}]已经存在，请重新填写。") ;
                exit; 
            }
            if (strlen($field['eventBagName']) ==0) {
                echo $this->view->message('请输入事件别名。') ;
                exit;
            }
            if ($this->_childPactEventModel->isExistsByEventBagName($field['childVersion'],$field['eventBagName'], $row['id'])) {
                echo $this->view->message("协议版本[{$field['mainVersion']}]事件别名[{$field['eventBagName']}]已经存在，请重新填写。") ;
                exit; 
            }
            //修改
            $num = $this->_childPactEventModel->update($field, "`id` = {$id}");
            if ($num>0&&$row['eventId']!=$field['eventId']){
                //更新id字段
                $element = array();
                $element['eventId'] = $field['eventId'];
                $this->_childPactEventFieldModel->update($element, "`childVersion`='{$field['childVersion']}' AND `eventId` = {$row['eventId']}");
                $this->_childPactEventFieldEnumModel->update($element, "`childVersion`='{$field['childVersion']}' AND `eventId` = {$row['eventId']}");
                $this->_childPactEventFieldLoopModel->update($element, "`childVersion`='{$field['childVersion']}' AND `eventId` = {$row['eventId']}");
            }
            
            $passwordMsg = ",协议版本[{$field['childVersion']}]事件[{$field['eventId']}]信息已更新";

            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功{$passwordMsg}。", $backUrl) ;
            exit;
        }
    }
   /**
     * 删除动作
     * 
     * @return void
     */
    public function childEventDeleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            //清空字段
            foreach ($ids as $id){
                $row = $this->_childPactEventModel->fetchRow("`id` = {$id}");
                if ($row){
                    $this->_childPactEventFieldModel->delete("`childVersion`='{$row['childVersion']}' AND `eventId`={$row['eventId']}");
                    $this->_childPactEventFieldEnumModel->delete(" `childVersion`='{$row['childVersion']}' AND `eventId`={$row['eventId']}");
                    $this->_childPactEventFieldLoopModel->delete(" `childVersion`='{$row['childVersion']}' AND `eventId`={$row['eventId']}");
                }
            }
            //删除事件
            if (is_array($ids)) {
                $this->_childPactEventModel->deleteByIds($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
   /**
     * 主协议事件字段列表
     * 
     * @return void
     */
    public function childEventFieldAction()
    {
        //获取当前页码
        $pageSize = 2000;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));

        $urls = $vars;
        if ($vars['childVersion'] != '') {
            $wheres[] = "`childVersion`= '{$vars['childVersion']}'";
        }
        if ($vars['eventId'] != '') {
            $wheres[] = "`eventId`= '{$vars['eventId']}'";
        }
        $orderBy = ' sort ASC';

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT *,CONVERT(fieldSort,SIGNED) as sort FROM `{$this->_childPactEventFieldModel->getTableName()}`{$where} ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        
        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
        
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
    }
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getChildPactEventFieldSortIsExistsByJsonAction()
    {
        $childVersion = trim($this->_request->get('childVersion'));
        $eventId = trim($this->_request->get('eventId'));
        $account = trim($this->_request->get('account'));
        $id = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_childPactEventFieldModel->isExists($childVersion,$eventId,$account, $id);
        }
        exit(Zend_Json::encode($isExists));
    }
   /**
     * 添加动作
     * 
     * @return void
     */
    public function childEventFieldInsertAction()
    {
        $vars = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        //最大序号
        $maxSort = $this->_childPactEventFieldModel->getMaxSort($vars['childVersion'], $vars['eventId']);
        $this->view->maxSort = $maxSort+1;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['childVersion'] = $filter->filter(trim($this->_request->getPost('childVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['fieldName'] = $filter->filter(trim($this->_request->getPost('fieldName')));
            $field['fieldSort'] = (int)$filter->filter(trim($this->_request->getPost('fieldSort')));
            $field['complyMode'] = (int)$filter->filter(trim($this->_request->getPost('complyMode')));
            $field['fieldType'] = $filter->filter(trim($this->_request->getPost('fieldType')));
            $field['addTime']= time();
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));

            if (strlen($field['fieldSort']) ==0) {
                echo $this->view->message("请输入事件[{$field['eventId']}]的字段序号。") ;
                exit;
            }
            if ($this->_childPactEventFieldModel->isExists($field['childVersion'],$field['eventId'],$field['fieldSort'])) {
                echo $this->view->message("协议版本{$field['childVersion']}事件[{$field['eventId']}]的字段序号已经存在，请重新填写。") ;
                exit; 
            }
            $id = $this->_childPactEventFieldModel->insert($field);

            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'child-event-field','childVersion' => $field['childVersion'],'eventId'=>$field['eventId']))) ;
            exit;
        }
    }
    
   /**
     * 更新动作
     * 
     * @return void
     */
    public function childEventFieldUpdateAction()
    {
        $vars = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_childPactEventFieldModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;

        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['childVersion'] = $filter->filter(trim($this->_request->getPost('childVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['fieldName'] = $filter->filter(trim($this->_request->getPost('fieldName')));
            $field['fieldSort'] = (int)$filter->filter(trim($this->_request->getPost('fieldSort')));
            $field['complyMode'] = (int)$filter->filter(trim($this->_request->getPost('complyMode')));
            $field['fieldType'] = $filter->filter(trim($this->_request->getPost('fieldType')));
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));
            $field['remark'] = $filter->filter(trim($this->_request->getPost('remark')));
            
            if (strlen($field['fieldSort']) ==0) {
                echo $this->view->message("请输入事件[{$field['eventId']}]的字段序号。") ;
                exit;
            }
            if ($this->_childPactEventFieldModel->isExists($field['childVersion'],$field['eventId'],$field['fieldSort'], $row['id'])) {
                echo $this->view->message("协议版本[{$field['childVersion']}]事件[{$field['eventId']}]的字段序号[{$field['fieldSort']}]已经存在，请重新填写。") ;
                exit; 
            }
            //修改
            $num = $this->_childPactEventFieldModel->update($field, "`id` = {$id}");
            if ($num>0&&$row['fieldSort']!=$field['fieldSort']){
                //更新枚举类型
                $element = array();
                $element['fieldSort'] = $field['fieldSort'];
                $this->_childPactEventFieldEnumModel->update($element, "`childVersion`='{$row['childVersion']}' AND `eventId` = {$row['eventId']} AND `fieldSort` = {$row['fieldSort']}");
                $this->_childPactEventFieldLoopModel->update($element, "`childVersion`='{$row['childVersion']}' AND `eventId` = {$row['eventId']} AND `fieldSort` = {$row['fieldSort']}");
            }
            
            $passwordMsg = ",协议版本[{$field['childVersion']}]事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]信息已更新";

            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功{$passwordMsg}。", $backUrl) ;
            exit;
        }
    }
    
   /**
     * 删除动作
     * 
     * @return void
     */
    public function childEventFieldDeleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            foreach ($ids as $id){
                $row = $this->_childPactEventFieldModel->fetchRow("`id` = {$id}");
                if ($row){
                    $this->_childPactEventFieldEnumModel->delete(" `mainVersion`='{$row['mainVersion']}' AND `eventId`={$row['eventId']} AND `fieldSort` ={$row['fieldSort']}");
                    $this->_childPactEventFieldLoopModel->delete(" `mainVersion`='{$row['mainVersion']}' AND `eventId`={$row['eventId']} AND `fieldSort` ={$row['fieldSort']}");
                }
            }
            if (is_array($ids)) {
                $this->_childPactEventFieldModel->deleteByIds($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
    
/**
     * 分支协议Id定义列表
     * 
     * @return void
     */
    public function childIdDefineAction()
    {
        //获取当前页码
        $pageSize = 2000;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['fieldType'] = trim($this->_request->get('fieldType'))? trim($this->_request->get('fieldType')):'C';

        $urls = $vars;
        if ($vars['childVersion'] != '') {
            $wheres[] = "`childVersion`= '{$vars['childVersion']}'";
        }
        if ($vars['fieldType'] != '') {
            $wheres[] = "`fieldType`= '{$vars['fieldType']}'";
        }
        $orderBy = '`fieldType` DESC,`pageId` ASC';

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_childPactIdDefineModel->getTableName()}`{$where} ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        
        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
    }
    
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getChildPactPageIdIsExistsByJsonAction()
    {
        $childVersion = trim($this->_request->get('childVersion'));
        $mainVersion = $this->_childPactModel->getMainVersion($childVersion);
        $fieldType = trim($this->_request->get('fieldType'));
        $account = trim($this->_request->get('account'));
        $id = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_childPactIdDefineModel->isExists($mainVersion,$childVersion,$fieldType,$account, $id);
        }
        exit(Zend_Json::encode($isExists));
    }
    
    
   /**
     * 添加动作
     * 
     * @return void
     */
    public function childIdDefineInsertAction()
    {
        $vars = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['fieldType'] = trim($this->_request->get('fieldType'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['childVersion'] = $filter->filter(trim($this->_request->getPost('childVersion')));
            $field['fieldType'] = trim($this->_request->getPost('fieldType'));
            $field['pageId'] = (int)trim($this->_request->getPost('pageId'));
            $field['childPageName'] = $filter->filter(trim($this->_request->getPost('childPageName')));
            $field['relationId'] = $filter->filter(trim($this->_request->getPost('relationId')));
            $field['aidNote'] = $filter->filter(trim($this->_request->getPost('aidNote')));
            $field['isUse'] = $this->_request->getPost('isUse');
            if (!$field['isUse']){
                $field['isUse']=0;
            }
            $field['isUseBi'] = $this->_request->getPost('isUseBi');
            if (!$field['isUseBi']){
                $field['isUseBi']=0;
            }
            $field['isUseManager'] = $this->_request->getPost('isUseManager');
            if (!$field['isUseManager']){
                $field['isUseManager']=0;
            }
            $field['isUserNow'] = $this->_request->getPost('isUserNow');
            if (!$field['isUserNow']){
                $field['isUserNow']=0;
            }
            $field['isFlag'] = $this->_request->getPost('isFlag');
            if (!$field['isFlag']){
                $field['isFlag']=0;
            }
            $field['addTime']= time();
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));
            $field['remark'] = $filter->filter(trim($this->_request->getPost('remark')));

            if (strlen($field['pageId']) ==0) {
                echo $this->view->message('请输入Id值。') ;
                exit;
            }
            $mainVersion = $this->_childPactModel->getMainVersion($field['childVersion']);
            $isExists = $this->_childPactIdDefineModel->isExists($mainVersion,$field['childVersion'],$field['fieldType'],$field['pageId']);
            if ($isExists==1) {
                echo $this->view->message('此协议版本此Id值已经存在，请重新填写。') ;
                exit; 
            } else if ($isExists==2) {
                echo $this->view->message('主协议版本无此Id值，请更新主协议。') ;
                exit; 
            }
            $id = $this->_childPactIdDefineModel->insert($field);

            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'child-id-define','childVersion' => $field['childVersion'],'fieldType'=>$field['fieldType']))) ;
            exit;
        }
    }
    
   /**
     * 更新动作
     * 
     * @return void
     */
    public function childIdDefineUpdateAction()
    {
        $vars = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['fieldType'] = trim($this->_request->get('fieldType'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
        
        
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_childPactIdDefineModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;

        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['childVersion'] = $filter->filter(trim($this->_request->getPost('childVersion')));
            $field['fieldType'] = trim($this->_request->getPost('fieldType'));
            $field['pageId'] = (int)trim($this->_request->getPost('pageId'));
            $field['childPageName'] = $filter->filter(trim($this->_request->getPost('childPageName')));
            $field['pageType'] = (int)trim($this->_request->getPost('pageType'));
            $field['relationId'] = $filter->filter(trim($this->_request->getPost('relationId')));
            $field['aidNote'] = $filter->filter(trim($this->_request->getPost('aidNote')));
            $field['isUse'] = $this->_request->getPost('isUse');
            if (!$field['isUse']){
                $field['isUse']=0;
            }
            $field['isUseBi'] = $this->_request->getPost('isUseBi');
            if (!$field['isUseBi']){
                $field['isUseBi']=0;
            }
            $field['isUseManager'] = $this->_request->getPost('isUseManager');
            if (!$field['isUseManager']){
                $field['isUseManager']=0;
            }
            $field['isUserNow'] = $this->_request->getPost('isUserNow');
            if (!$field['isUserNow']){
                $field['isUserNow']=0;
            }
            $field['isFlag'] = $this->_request->getPost('isFlag');
            if (!$field['isFlag']){
                $field['isFlag']=0;
            }
            //必埋
            if ($field['isUse']==1&&$field['isUseBi']==1&&$field['isUseManager']==1&&$field['isFlag']==1){
                $field['isUserNow']=1;
            }
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));
            $field['remark'] = $filter->filter(trim($this->_request->getPost('remark')));

            if (strlen($field['pageId']) ==0) {
                echo $this->view->message('请输入Id值。') ;
                exit;
            }
            $mainVersion = $this->_childPactModel->getMainVersion($field['childVersion']);
            $isExists = $this->_childPactIdDefineModel->isExists($mainVersion,$field['childVersion'],$field['fieldType'],$field['pageId'], $row['id']);
            if ($isExists==1) {
                echo $this->view->message("协议版本[{$field['childVersion']}]Id定义值[{$field['pageId']}]已经存在，请重新填写。") ;
                exit; 
            } else if ($isExists==2) {
                echo $this->view->message('主协议版本无此Id值，请更新主协议。') ;
                exit; 
            }
            //修改
            $this->_childPactIdDefineModel->update($field, "`id` = {$id}");
            $passwordMsg = ",协议版本[{$field['childVersion']}]Id定义值[{$field['pageId']}]信息已更新";

            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功{$passwordMsg}。", $backUrl) ;
            exit;
        }
    }
   /**
     * 删除动作
     * 
     * @return void
     */
    public function childIdDefineDeleteAction()
    {
        if ($this->_request->isPost()) {
            $doType = $this->_request->getPost('doType');
            $ids= $this->_request->getPost('selectId');
            if (is_array($ids)) {
                if ($doType=='del'){
                    $this->_childPactIdDefineModel->deleteByIds($ids);
                } else if ($doType=='isUseManager-yes'){
                    $this->_childPactIdDefineModel->updateByIds('isUseManager',1,$ids);         
                } else if ($doType=='isUseManager-no'){
                    $this->_childPactIdDefineModel->updateByIds('isUseManager',0,$ids);
                } else if ($doType=='isUserNow-yes'){
                    $this->_childPactIdDefineModel->updateByIds('isUserNow',1,$ids);
                } else if ($doType=='isUserNow-no'){
                    $this->_childPactIdDefineModel->updateByIds('isUserNow',0,$ids);
                } else{
                    echo $this->view->message('非法操作!') ;
                    exit;
                }
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
    /**
     * 分支协议列表
     * 
     * @return void
     */
    public function listAction()
    {
        //获取当前页码
        $pageSize = 30;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();
        $wheres[] = "`productCode` = '{$this->_productCode}'";
        $wheres[] = "`childStatus` = 1";//当前分支状态=>上线
        
        $urls = $vars;

        $orderBy = '`addTime` DESC';

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_childPactModel->getTableName()}`{$where} ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        
        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
    }
    
    /**
     * 默认动作
     * 
     * @return void
     */
    public function listShowAction()
    {
        $vars = array();
        $wheres = array();
        
        $vars['id'] = intval($this->_request->get('id'));
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        
        if (empty($vars['id'])) {
             exit("没有传递参数ID");
        }
        $row = $this->_childPactModel->fetchRow("`id` = {$vars['id']}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;
        
        if ($vars['childVersion'] != '') {
            $wheres[] = "`childVersion`= '{$vars['childVersion']}'";
        }
        
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        //事件列表
        $sql = "SELECT * FROM `{$this->_childPactEventModel->getTableName()}`{$where} ORDER BY `eventId` ASC";
        $eventRows = $this->_childPactEventModel->getAdapter()->fetchAll($sql);
        $this->view->eventRows = $eventRows;
        
        //id定义列表
        $sql = "SELECT * FROM `{$this->_childPactIdDefineModel->getTableName()}`{$where} ORDER BY `fieldType` DESC,`pageId` ASC";
        $idDefineRows = $this->_childPactIdDefineModel->getAdapter()->fetchAll($sql);
        $this->view->idDefineRows = $idDefineRows;
        
        //页面ID列表
        $sql = "SELECT * FROM `{$this->_childPactPageIdModel->getTableName()}`{$where} ORDER BY `fieldType` DESC,`pageId` ASC";
        $pageIdRows = $this->_childPactPageIdModel->getAdapter()->fetchAll($sql);
        $this->view->pageIdRows = $pageIdRows;
        
        $this->view->vars = $vars;

    }
    
    
    
   /**
     * 分支协议事件枚举值
     * 
     * @return void
     */
    public function childFieldEnumAction()
    {
        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['fieldSort'] = trim($this->_request->get('fieldSort'));
        
        $urls = $vars;
        if ($vars['childVersion'] != '') {
            $wheres[] = "`childVersion`= '{$vars['childVersion']}'";
        }
        if ($vars['eventId'] != '') {
            $wheres[] = "`eventId`= {$vars['eventId']}";
        }
        if ($vars['fieldSort'] != '') {
            $wheres[] = "`fieldSort`= {$vars['fieldSort']}";
        }
        $orderBy = '`enum` ASC';

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_childPactEventFieldEnumModel->getTableName()}`{$where} ORDER BY {$orderBy}";
        $rows = $this->_childPactEventFieldEnumModel->getAdapter()->fetchAll($sql);

        $this->view->rows = $rows;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
    }
    
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getEventFieldEnumIsExistsByJsonAction()
    {
        $childVersion = trim($this->_request->get('childVersion'));
        $eventId = trim($this->_request->get('eventId'));
        $fieldSort = trim($this->_request->get('fieldSort'));
        $account = trim($this->_request->get('account'));
        $id = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_childPactEventFieldEnumModel->isExists($childVersion,$eventId,$fieldSort,$account,$id);
        }
        exit(Zend_Json::encode($isExists));
    }
    
   /**
     * 添加动作
     * 
     * @return void
     */
    public function childFieldEnumInsertAction()
    {
        $vars = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['fieldSort'] = trim($this->_request->get('fieldSort'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        $this->view->enumAuthor= Application_Model_UserLogin::getInstance()->getLoginedUserName();
        
        $sql = "SELECT * FROM `{$this->_childPactEventFieldModel->getTableName()}` WHERE `childVersion`='{$vars['childVersion']}' AND `eventId`={$vars['eventId']} AND `fieldSort`={$vars['fieldSort']}";
        $eventFieldRow = $this->_childPactEventFieldModel->getAdapter()->fetchRow($sql);
        $fieldType = $eventFieldRow['fieldType'];
        $this->view->fieldType = $fieldType;
        
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['childVersion'] = $filter->filter(trim($this->_request->getPost('childVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['fieldSort'] = (int)$filter->filter(trim($this->_request->getPost('fieldSort')));
            $field['enum'] = (int)$filter->filter(trim($this->_request->getPost('enum')));
            $field['enumValue'] = $filter->filter(trim($this->_request->getPost('enumValue')));
            $field['enumAuthor'] = $filter->filter(trim($this->_request->getPost('enumAuthor')));
            $field['addTime']= time();
            $field['remark'] = $filter->filter(trim($this->_request->getPost('remark')));

            if (strlen($field['childVersion']) ==0||strlen($field['eventId']) ==0||strlen($field['fieldSort']) ==0) {
                echo $this->view->message("非法操作!") ;
                exit;
            }
            
            $field['fieldType'] = $fieldType; 
            
            if (in_array($field['fieldType'],array('E','N'))){
	            if (!($field['enum']>=0&&$field['enum']<=127)){
	                echo $this->view->message("枚举键范围[0-127],请重新输入!") ;
	                exit;
	            }
            }
            if (in_array($field['fieldType'],array('F','G'))){
	            if (!($field['enum']>=0&&$field['enum']<=32767)){
	                echo $this->view->message("枚举键范围[0-32767],请重新输入!") ;
	                exit;
	            }
            }
            if ($this->_childPactEventFieldEnumModel->isExists($field['childVersion'],$field['eventId'],$field['fieldSort'],$field['enum'])) {
                echo $this->view->message("协议版本{$field['childVersion']}事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]的枚举键已经存在，请重新填写。") ;
                exit; 
            }
            
            
            $id = $this->_childPactEventFieldEnumModel->insert($field);

            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'child-field-enum','childVersion' => $field['childVersion'],'eventId'=>$field['eventId'],'fieldSort'=>$field['fieldSort']))) ;
            exit;
        }
    }
   /**
     * 更新动作
     * 
     * @return void
     */
    public function childFieldEnumUpdateAction()
    {
        $vars = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['fieldSort'] = trim($this->_request->get('fieldSort'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_childPactEventFieldEnumModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;
       
        $sql = "SELECT * FROM `{$this->_childPactEventFieldModel->getTableName()}` WHERE `childVersion`='{$vars['childVersion']}' AND `eventId`={$vars['eventId']} AND `fieldSort`={$vars['fieldSort']}";
        $eventFieldRow = $this->_childPactEventFieldModel->getAdapter()->fetchRow($sql);
        $fieldType = $eventFieldRow['fieldType'];
        $this->view->fieldType = $fieldType;
        
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['childVersion'] = $filter->filter(trim($this->_request->getPost('childVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['fieldSort'] = (int)$filter->filter(trim($this->_request->getPost('fieldSort')));
            $field['enum'] = (int)$filter->filter(trim($this->_request->getPost('enum')));
            $field['enumValue'] = $filter->filter(trim($this->_request->getPost('enumValue')));
            $field['remark'] = $filter->filter(trim($this->_request->getPost('remark')));
            $field['fieldType'] = $row['fieldType'];
            
            if (strlen($field['childVersion']) ==0||strlen($field['eventId']) ==0||strlen($field['fieldSort']) ==0) {
                echo $this->view->message("非法操作!") ;
                exit;
            }
            
            if (!$row['fieldType']){
                $field['fieldType'] = $fieldType; 
            }
            
            if (in_array($field['fieldType'],array('E','N'))){
	            if (!($field['enum']>=0&&$field['enum']<=127)){
	                echo $this->view->message("枚举键范围[0-127],请重新输入!") ;
	                exit;
	            }
            }
            if (in_array($field['fieldType'],array('F','G'))){
	            if (!($field['enum']>=0&&$field['enum']<=32767)){
	                echo $this->view->message("枚举键范围[0-32767],请重新输入!") ;
	                exit;
	            }
            }
            if ($this->_childPactEventFieldEnumModel->isExists($field['childVersion'],$field['eventId'],$field['fieldSort'],$field['enum'], $row['id'])) {
                echo $this->view->message("协议版本{$field['childVersion']}事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]的枚举键已经存在，请重新填写。") ;
                exit; 
            }
            
            //修改
            $this->_childPactEventFieldEnumModel->update($field, "`id` = {$id}");
            $passwordMsg = ",协议版本[{$field['childVersion']}]事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]枚举信息已更新";

            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功{$passwordMsg}。", $backUrl) ;
            exit;
        }
    }
   /**
     * 删除动作
     * 
     * @return void
     */
    public function childFieldEnumDeleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            //删除id值
            if (is_array($ids)) {
                $this->_childPactEventFieldEnumModel->deleteByIds($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
    
    
    /**
     * 页面id
     * 
     * @return void
     */
    public function childPageIdAction()
    {
        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['fieldType'] = trim($this->_request->get('fieldType'));
        $urls = $vars;
        if ($vars['childVersion'] != '') {
            $wheres[] = "`childVersion`= '{$vars['childVersion']}'";
        }
        $orderBy = '`pageId` ASC';

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_childPactPageIdModel->getTableName()}`{$where} ORDER BY {$orderBy}";
        $rows = $this->_childPactPageIdModel->getAdapter()->fetchAll($sql);

        $this->view->rows = $rows;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
    }
    
    /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getPageIdIsExistsByJsonAction()
    {
        $childVersion = trim($this->_request->get('childVersion'));
        $account = trim($this->_request->get('account'));
        $id = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_childPactPageIdModel->isExists($childVersion,$account,$id);
        }
        exit(Zend_Json::encode($isExists));
    }
    
    
    /**
     * 添加动作
     * 
     * @return void
     */
    public function childPageIdInsertAction()
    {
        $vars = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['fieldType'] = trim($this->_request->get('fieldType'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['childVersion'] = $filter->filter(trim($this->_request->getPost('childVersion')));
            $field['fieldType'] = trim($this->_request->getPost('fieldType'));
            $field['pageId'] = trim($this->_request->getPost('pageId'));
            $field['pageName'] = $filter->filter(trim($this->_request->getPost('pageName')));
            $field['relationId'] = $filter->filter(trim($this->_request->getPost('relationId')));
            $field['relationId'] = $field['relationId'] ? $field['relationId'] : $field['pageId'];
            $field['isMd'] = $this->_request->getPost('isMd');
            if (!$field['isMd']){
                $field['isMd']=0;
            }
            $field['isUseBi'] = $this->_request->getPost('isUseBi');
            if (!$field['isUseBi']){
                $field['isUseBi']=0;
            }
            $field['isUseManager'] = $this->_request->getPost('isUseManager');
            if (!$field['isUseManager']){
                $field['isUseManager']=0;
            }
            $field['isUserNow'] = $this->_request->getPost('isUserNow');
            if (!$field['isUserNow']){
                $field['isUserNow']=0;
            }
            $field['isFlag'] = $this->_request->getPost('isFlag');
            if (!$field['isFlag']){
                $field['isFlag']=0;
            }
            $field['isLock'] = $this->_request->getPost('isLock');
            if (!$field['isLock']){
                $field['isLock']=0;
            }
            $field['addTime']= time();
            $field['mdVersion'] = $filter->filter(trim($this->_request->getPost('mdVersion')));
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));
            $field['remark'] = $filter->filter(trim($this->_request->getPost('remark')));
            
            if (strlen($field['pageId']) ==0) {
                echo $this->view->message('请输入页面ID。') ;
                exit;
            }
            $isExists = $this->_childPactPageIdModel->isExists($field['childVersion'],$field['pageId']);
            if ($isExists==1) {
                echo $this->view->message("协议版本[{$field['childVersion']}]的页面ID已经存在，请重新填写。") ;
                exit; 
            }
            $id = $this->_childPactPageIdModel->insert($field);

            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'child-page-id','childVersion' => $field['childVersion'],'fieldType'=>$field['fieldType']))) ;
            exit;
        }
    }
    
   /**
     * 更新动作
     * 
     * @return void
     */
    public function childPageIdUpdateAction()
    {
        $vars = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['fieldType'] = trim($this->_request->get('fieldType'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
        
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_childPactPageIdModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;

        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['childVersion'] = $filter->filter(trim($this->_request->getPost('childVersion')));
            $field['fieldType'] = trim($this->_request->getPost('fieldType'));
            $field['pageId'] = trim($this->_request->getPost('pageId'));
            $field['pageName'] = $filter->filter(trim($this->_request->getPost('pageName')));
            $field['pageType'] = (int)trim($this->_request->getPost('pageType'));
            $field['relationId'] = $filter->filter(trim($this->_request->getPost('relationId')));
            $field['isMd'] = $this->_request->getPost('isMd');
            if (!$field['isMd']){
                $field['isMd']=0;
            }
            $field['isUseBi'] = $this->_request->getPost('isUseBi');
            if (!$field['isUseBi']){
                $field['isUseBi']=0;
            }
            $field['isUseManager'] = $this->_request->getPost('isUseManager');
            if (!$field['isUseManager']){
                $field['isUseManager']=0;
            }
            $field['isUserNow'] = $this->_request->getPost('isUserNow');
            if (!$field['isUserNow']){
                $field['isUserNow']=0;
            }
            $field['isFlag'] = $this->_request->getPost('isFlag');
            if (!$field['isFlag']){
                $field['isFlag']=0;
            }
            $field['isLock'] = $this->_request->getPost('isLock');
            if (!$field['isLock']){
                $field['isLock']=0;
            }
            $field['mdVersion'] = $filter->filter(trim($this->_request->getPost('mdVersion')));
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));
            $field['remark'] = $filter->filter(trim($this->_request->getPost('remark')));
            

            if (strlen($field['pageId']) ==0) {
                echo $this->view->message('请输入Id值。') ;
                exit;
            }
            $isExists = $this->_childPactPageIdModel->isExists($field['childVersion'],$field['pageId'], $row['id']);
            if ($isExists==1) {
                echo $this->view->message("协议版本[{$field['childVersion']}]的页面ID[{$field['pageId']}]已经存在，请重新填写。") ;
                exit; 
            }
            //修改
            $this->_childPactPageIdModel->update($field, "`id` = {$id}");
            $passwordMsg = ",协议版本[{$field['childVersion']}]的页面ID[{$field['pageId']}]信息已更新";

            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功{$passwordMsg}。", $backUrl) ;
            exit;
        }
    }
    
    
   /**
     * 批量动作
     * 
     * @return void
     */
    public function childPageIdDeleteAction()
    {
        if ($this->_request->isPost()) {
            $doType = $this->_request->getPost('doType');
            $ids= $this->_request->getPost('selectId');
            if (is_array($ids)) {
                if ($doType=='del'){
                    $this->_childPactPageIdModel->deleteByIds($ids);
                } else if ($doType=='isUseManager-yes'){
                    $this->_childPactPageIdModel->updateByIds('isUseManager',1,$ids);         
                } else if ($doType=='isUseManager-no'){
                    $this->_childPactPageIdModel->updateByIds('isUseManager',0,$ids);
                } else if ($doType=='isUserNow-yes'){
                    $this->_childPactPageIdModel->updateByIds('isUserNow',1,$ids);
                } else if ($doType=='isUserNow-no'){
                    $this->_childPactPageIdModel->updateByIds('isUserNow',0,$ids);
                } else if ($doType=='isLock-yes'){
                    $this->_childPactPageIdModel->updateByIds('isLock',1,$ids);
                } else if ($doType=='isLock-no'){
                    $this->_childPactPageIdModel->updateByIds('isLock',0,$ids);
                } else{
                    echo $this->view->message('非法操作!') ;
                    exit;
                }
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
    
/**
     * 主协议事件循环类型
     * 
     * @return void
     */
    public function childFieldLoopAction()
    {
        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['fieldSort'] = trim($this->_request->get('fieldSort'));
        
        $urls = $vars;
        if ($vars['childVersion'] != '') {
            $wheres[] = "`childVersion`= '{$vars['childVersion']}'";
        }
        if ($vars['eventId'] != '') {
            $wheres[] = "`eventId`= {$vars['eventId']}";
        }
        if ($vars['fieldSort'] != '') {
            $wheres[] = "`fieldSort`= {$vars['fieldSort']}";
        }
        $orderBy = '`loopFieldSort` ASC';

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_childPactEventFieldLoopModel->getTableName()}`{$where} ORDER BY {$orderBy}";
        $rows = $this->_childPactEventFieldLoopModel->getAdapter()->fetchAll($sql);

        $this->view->rows = $rows;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
        
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
    }
    
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getEventFieldLoopIsExistsByJsonAction()
    {
        $childVersion = trim($this->_request->get('childVersion'));
        $eventId = trim($this->_request->get('eventId'));
        $fieldSort = trim($this->_request->get('fieldSort'));
        $account = trim($this->_request->get('account'));
        $id = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_childPactEventFieldLoopModel->isExists($childVersion,$eventId,$fieldSort,$account,$id);
        }
        exit(Zend_Json::encode($isExists));
    }
    
    
    /**
     * 添加动作
     * 
     * @return void
     */
    public function childFieldLoopInsertAction()
    {
        $vars = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['fieldSort'] = trim($this->_request->get('fieldSort'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        
        //最大序号
        $maxSort = $this->_childPactEventFieldLoopModel->getMaxSort($vars['childVersion'], $vars['eventId'],$vars['fieldSort']);
        $this->view->maxSort = $maxSort+1;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['childVersion'] = $filter->filter(trim($this->_request->getPost('childVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['fieldSort'] = (int)$filter->filter(trim($this->_request->getPost('fieldSort')));
            $field['loopFieldSort'] = (int)$filter->filter(trim($this->_request->getPost('loopFieldSort')));
            $field['fieldName'] = $filter->filter(trim($this->_request->getPost('fieldName')));
            $field['fieldType'] = $filter->filter(trim($this->_request->getPost('fieldType')));
            $field['complyMode'] = $filter->filter(trim($this->_request->getPost('complyMode')));
            $field['addTime']= time();
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));
            $field['remark'] = $filter->filter(trim($this->_request->getPost('remark')));

            if (strlen($field['childVersion']) ==0||strlen($field['eventId']) ==0||strlen($field['fieldSort']) ==0) {
                echo $this->view->message("非法操作!") ;
                exit;
            }
            if (!($field['loopFieldSort']>0)){
                echo $this->view->message("字段序号有误,请重新输入!") ;
                exit;
            }
            if ($this->_childPactEventFieldLoopModel->isExists($field['childVersion'],$field['eventId'],$field['fieldSort'],$field['loopFieldSort'])) {
                echo $this->view->message("协议版本{$field['childVersion']}事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]的字段序号已经存在，请重新填写。") ;
                exit; 
            }
            $id = $this->_childPactEventFieldLoopModel->insert($field);

            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'child-field-loop','childVersion' => $field['childVersion'],'eventId'=>$field['eventId'],'fieldSort'=>$field['fieldSort']))) ;
            exit;
        }
    }
    
    /**
     * 更新动作
     * 
     * @return void
     */
    public function childFieldLoopUpdateAction()
    {
        $vars = array();
        $vars['childVersion'] = trim($this->_request->get('childVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['fieldSort'] = trim($this->_request->get('fieldSort'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_childPactEventFieldLoopModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;
        
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
        
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['childVersion'] = $filter->filter(trim($this->_request->getPost('childVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['fieldSort'] = (int)$filter->filter(trim($this->_request->getPost('fieldSort')));
            $field['loopFieldSort'] = (int)$filter->filter(trim($this->_request->getPost('loopFieldSort')));
            $field['fieldName'] = $filter->filter(trim($this->_request->getPost('fieldName')));
            $field['fieldType'] = $filter->filter(trim($this->_request->getPost('fieldType')));
            $field['complyMode'] = $filter->filter(trim($this->_request->getPost('complyMode')));
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));
            $field['remark'] = $filter->filter(trim($this->_request->getPost('remark')));
            
            if (strlen($field['childVersion']) ==0||strlen($field['eventId']) ==0||strlen($field['fieldSort']) ==0) {
                echo $this->view->message("非法操作!") ;
                exit;
            }
            if (!($field['loopFieldSort']>0)){
                echo $this->view->message("字段序号有误,请重新输入!") ;
                exit;
            }
            if ($this->_childPactEventFieldLoopModel->isExists($field['childVersion'],$field['eventId'],$field['fieldSort'],$field['loopFieldSort'], $row['id'])) {
                echo $this->view->message("协议版本{$field['childVersion']}事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]的循环的字段序号[{$field['loopFieldSort']}]已经存在，请重新填写。") ;
                exit; 
            }
            //修改
            $this->_childPactEventFieldLoopModel->update($field, "`id` = {$id}");
            
            $passwordMsg = ",协议版本[{$field['childVersion']}]事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]循环的字段序号[{$field['loopFieldSort']}]信息已更新";

            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功{$passwordMsg}。", $backUrl) ;
            exit;
        }
    }
    
    /**
     * 删除动作
     * 
     * @return void
     */
    public function childFieldLoopDeleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            if (is_array($ids)) {
                $this->_childPactEventFieldLoopModel->deleteByIds($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
    
    
     /**
     * 编译事件包
     * 
     * @return void
     */
    public function childEventBagAction()
    {
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_childPactModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;
        
        //开始编译
        $pythonPath = "/var/www/python/BIEventMaker.py";//"E:\\python\\www\\bi_event\\BIEventMaker.py";
        $phpPath = $_SERVER['DOCUMENT_ROOT'];//"E:\\php\\www\\yek_mob";
        $pythonVar = array();
        //1:子协议版本号  
        $pythonVar['childVersion']= $row['childVersion'];
        //2： 工作目录  
        $pythonVar['workPath']="{$phpPath}/data/md/{$row['childVersion']}";
        //3：源文件打包后 jar文件路径  
        $pythonVar['sourceFile']= "{$phpPath}/data/md/{$row['childVersion']}/{$row['childVersion']}-src.jar";
        //4：类文件打包后jar文件路径
        $pythonVar['classFile']= "{$phpPath}/data/md/{$row['childVersion']}/{$row['childVersion']}.jar";
        $pythonVars = count($pythonVar) > 0 ? '' . implode(' ', $pythonVar) . '' : '';
        $lastLine = file_get_contents("http://u.yekmob.com/service/bievent?ver=" . urlencode($pythonVar['childVersion']) . "&doc=" . urlencode($pythonVar['workPath']) . "&srcjar=" . urlencode($pythonVar['sourceFile']) . "&classjar=" . urlencode($pythonVar['classFile']));
        /*
        $lastLine = exec('java');//exec("python {$pythonPath} {$pythonVars}");//disable_functions 
        */
        $vars = $pythonVar;
        $vars['flag'] = false;
        $vars['sourceFile'] = str_replace("\\","/",str_replace($phpPath, '',$pythonVar['sourceFile']));
        $vars['classFile'] = str_replace("\\","/",str_replace($phpPath, '',$pythonVar['classFile']));
		if($lastLine == 'OK'){
		    $vars['flag'] = true;
		}
        $this->view->vars = $vars;

    }
    
   /**
     * 导出协议
     * 
     * @return void
     */
    public function childEventExportAction()
    {
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $cRow = $this->_childPactModel->fetchRow("`id` = {$id}"); 
        if (empty($cRow)) {
             exit("没有找到记录。");
        }
        //主协议
        $mRow = $this->_mainPactModel->fetchRow("`mainVersion` = '{$cRow['mainVersion']}'");
        //类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        
        
        $datas = array();
        //协议版本号
        $datas ['version'] = $cRow['childVersion'];
        //系统保留ID区间
        $datas ['keepID']['start'] = $mRow['keepStartEventId'];
        $datas ['keepID']['end'] = $mRow['keepEndEventId'];
        //用户自定义ID区间
        $datas ['userDefID']['start'] = $mRow['userDefineStartEventId'];
        $datas ['userDefID']['end'] = $mRow['userDefineEndEventId'];
        //数据类型定义
        $i=0;
        foreach ($fieldTypeRows as $row){
        	//类型编码
        	$datas ['types'][$i]['code'] = $row['code'];
        	//基类型编码
        	$datas ['types'][$i]['type'] = $row['type'];
        	//编码全称
        	$datas ['types'][$i]['name'] = $row['name'];
        	//备注
        	$datas ['types'][$i]['note'] = $row['note'];
        	++$i;
        }
        
        //事件
        $cEventRows = $this->_childPactEventModel->fetchAll("`childVersion` = '{$cRow['childVersion']}'");
        foreach ($cEventRows as $key => $row){
        	//事件ID 
        	$datas ['events'][$key]['id'] = $row['eventId'];
        	//事件名称
        	$datas ['events'][$key]['name'] = $row['eventName'];
        	//事件的类名称 
        	$datas ['events'][$key]['className'] = $row['eventBagName'];
        	//事件的实现方式 SDK/APP,SDK代表需由SDK实现
        	$datas ['events'][$key]['codeType'] = $row['eventComplyMode']==1 ? 'APP':'SDK';
        	//事件埋点的时机
        	$datas ['events'][$key]['eventPoint'] = $row['eventPoint'];
        	//备注
        	$datas ['events'][$key]['comment'] = trim($row['note']);
        	//字段列表
        	$cEventFieldRows = $this->_childPactEventFieldModel->fetchAll("`childVersion` = '{$cRow['childVersion']}' and `eventId`='{$row['eventId']}'");
        	foreach ($cEventFieldRows as $k=>$v){
        		//字段在事件中的排序
        		$datas ['events'][$key]['fields'][$k]['sort'] = $v['fieldSort'];
        		//字段名
        		$datas ['events'][$key]['fields'][$k]['name'] = $v['fieldName'];
        		//字段的基本类型
        		$datas ['events'][$key]['fields'][$k]['type'] = $v['fieldType'];
        		//字段的编码实现方式
        		$datas ['events'][$key]['fields'][$k]['codeType'] = $v['complyMode']==1 ? 'APP':'SDK';
        		//字段的说明
        		$datas ['events'][$key]['fields'][$k]['note'] = trim($v['note']);
        		//字段的备注
        		$datas ['events'][$key]['fields'][$k]['comment'] = trim($v['remark']);
        		
        	    //循环 类型
                if ($v['fieldType']=='K'){
                    $eflRows = $this->_childPactEventFieldLoopModel ->fetchAll("childVersion='{$v['childVersion']}' AND eventId ={$v['eventId']} AND fieldSort={$v['fieldSort']}");
                    foreach ($eflRows as $val =>$fieldLoop){
                    	//字段在循环中的排序
	                   $datas ['events'][$key]['fields'][$k]['loop'][$val]['sort'] = $fieldLoop['loopFieldSort'];
	                   //字段名
	                   $datas ['events'][$key]['fields'][$k]['loop'][$val]['name'] = $fieldLoop['fieldName'];
	                   //字段的基本类型
	                   $datas ['events'][$key]['fields'][$k]['loop'][$val]['type'] = $fieldLoop['fieldType'];
	                   //字段的编码实现方式
	                   $datas ['events'][$key]['fields'][$k]['loop'][$val]['codeType'] = $fieldLoop['complyMode']==1 ? 'APP':'SDK';
	                   //说明
	                   $datas ['events'][$key]['fields'][$k]['loop'][$val]['note'] = trim($fieldLoop['note']);
	                   //备注
	                   $datas ['events'][$key]['fields'][$k]['loop'][$val]['comment'] = trim($fieldLoop['remark']);
                    }
                }
                
                //枚举 类型
                if (in_array($v['fieldType'], array('E','N','F','G'))){
                    $efeRows = $this->_childPactEventFieldEnumModel ->fetchAll("childVersion='{$v['childVersion']}' AND eventId ={$v['eventId']} AND fieldSort={$v['fieldSort']} AND fieldType='{$v['fieldType']}'");
                    foreach ($efeRows as $val =>$fieldEnum){
                       //枚举的键
	                   $datas ['events'][$key]['fields'][$k]['enum'][$val]['key'] = $fieldEnum['enum'];
	                   //枚举的值
	                   $datas ['events'][$key]['fields'][$k]['enum'][$val]['value'] = $fieldEnum['enumValue'];
                    }
                }
                
                //其它 类型
        		
        	}
        }
        
        
		$result = $datas;
		$result ['data'] = json_encode ( $result );
		echo $result ['data'];
		$outputFileName = $cRow['childVersion'].".json";
		//输出 
        header("Content-Type: application/force-download");  
        header("Content-Type: application/octet-stream");  
        header("Content-Type: application/download");  
        header('Content-Disposition:inline;filename="'.$outputFileName.'"');  
        header("Content-Transfer-Encoding: binary");  
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");  
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");  
        header("Pragma: no-cache");
        exit;
        
    }
    
    /**
     * 导出协议对照
     * 
     * @return void
     */
    public function childEventNoteAction()
    {
    	$this->view->show = 'on';
    }
    
    
    
}